<template>
  <el-container>
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/logo.png" alt="" />
        <span class="dszt">电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logintc">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏区域 -->
      <!-- 折叠完成   <el-aside :width="iszhedie ? '64px' : '200px'">-->
      <!-- 当前激活菜单的 index :default-active = "$route.path" -->
      <el-aside :width="iszhedie ? '64px' : '200px'">
        <div class="xian-button" @click="getzhedie">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          unique-opened
          :collapse="iszhedie"
          :collapse-transition="false"
          router
          :default-active="$route.path"
        >
          <!-- 是否开启折叠动画  :collapse-transition="false"-->
          <!-- 一级菜单 -->
          <!-- :index="item.id + '' -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuleft"
            :key="item.id"
          >
            <template slot="title">
              <!-- 一级菜单的图标 -->
              <i :class="iconlist[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + secitem.path"
              v-for="secitem in item.children"
              :key="secitem.id"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ secitem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: "Home",
  data() {
    return {
      // 左侧菜单数据
      menuleft: [],
      iconlist: {
        125: "el-icon-user-solid",
        103: "el-icon-s-check",
        101: "el-icon-s-shop",
        102: "el-icon-s-order",
        145: "el-icon-s-marketing",
      },
      //   是否折叠
      iszhedie: false,
    };
  },
  methods: {
    logintc() {
      // 清除token
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    // 获取所有的菜单
    async getallleft() {
      const { data: res } = await this.$http.get("menus");
      console.log(res);
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg);
      } else {
        this.menuleft = res.data;
      }
    },
    // 点击按钮 切换菜单的折叠 与 展开
    getzhedie() {
      this.iszhedie = !this.iszhedie;
    },
  },
  created() {
    this.getallleft();
  },
};
</script>

<style lang="less" scoped>
.el-header {
  background-color: #373d41;
  display: flex;
  padding-left: 0;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  font-size: 20px;
}
.dszt {
  position: relative;
  top: -18px;
  margin-left: 10px;
}
.el-aside {
  background-color: #333744;
  transition: all 0.5s;
}
.el-main {
  background-color: #eaedf1;
}
// 高度撑满
.el-container {
  height: 100%;
}
img {
  width: 50px;
  height: 50px;
}
.el-menu {
  border: 0;
}
.xian-button {
  background-color: #4a5064;
  font-size: 15px;
  line-height: 24px;
  text-align: center;
  color: #fff;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>